package com.youyun56.commons;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author Jet
 */
public class DateUtils {

	public final static String[] SUPPORTED_FORMATS = { "yyyy-MM-dd", "MM/dd/yyyy HH:mm:ss", "MM-dd-yyyy HH:mm:ss", "MM-dd-yyyy hh:mm:ss a", "MM-dd-yyyy", "MM-dd-yy", "MM/dd/yyyy", "MM.dd.yyyy",
			"MM.dd.yy", "MMddyyyy", "MMddyy" };
	public static SimpleDateFormat SHORT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat FULL_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static Date date(int year, int month, int day) {
		return date(year, month, day, 0, 0, 0);
	}

	public static Date date(int year, int month, int day, int hour, int minute, int second) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day, hour, minute, second);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	public static Date add(Date date, int field, int value) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(field, value);
		return calendar.getTime();
	}

	public static Calendar calendar(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return calendar;
	}

	public static int age(Date dateOfBirth, Date targetDate) {
		Calendar birth = calendar(dateOfBirth);
		Calendar target = calendar(targetDate);
		int age = target.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
		if ((birth.get(Calendar.MONTH) > target.get(Calendar.MONTH))
				|| (birth.get(Calendar.MONTH) == target.get(Calendar.MONTH) && birth.get(Calendar.DAY_OF_MONTH) > target.get(Calendar.DAY_OF_MONTH))) {
			age--;
		}
		return age;
	}

	public static String format(Date date, String format) {
		if (date == null) {
			return "";
		}
		return new SimpleDateFormat(format).format(date);
	}

	public static String formatShortDate(Date date) {
		return SHORT_DATE_FORMAT.format(date);
	}

	
	public static Date toDate(String string) {
		if (string == null) {
			return null;
		}
		string = string.trim();
		SimpleDateFormat sdf = null;
		Date one = null;
		for (String format : SUPPORTED_FORMATS) {
			sdf = new SimpleDateFormat(format);
			sdf.setLenient(false);
			try {
				one = sdf.parse(string);
				return one;
			} catch (ParseException e) {
			}
		}
		return null;
	}

	public static String getLastMonthFirstDay() {
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int year = 0;
		int month = cal.get(Calendar.MONTH); // 上个月月�?
		if (month == 0) {
			year = cal.get(Calendar.YEAR) - 1;
			month = 12;
		} else {
			year = cal.get(Calendar.YEAR);
		}
		String endDay = year + "-" + month + "-01";
		return endDay;
	}

	public static String getLastMonthLastDay() {
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int year = 0;
		int month = cal.get(Calendar.MONTH); // 上个月月�?
		// int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 结束天数
		int day = getLastMonthDayLenth();
		if (month == 0) {
			year = cal.get(Calendar.YEAR) - 1;
			month = 12;
		} else {
			year = cal.get(Calendar.YEAR);
		}
		String endDay = year + "-" + month + "-" + day;
		return endDay;
	}

	public static int getLastMonthDayLenth() {
		Calendar calendar = new GregorianCalendar();
		Calendar calendar1 = new GregorianCalendar(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) - 1, 1);
		return calendar1.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	// public static void main(String[] args) {
	// System.out.println(DateUtils.getLastMonthDayLenth());
	// }

	// 给定�?��日期型字符串，返回加减n天后的日期型字符�?
	public static String nDaysAfterOneDateString(String basicDate, int n) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date tmpDate = null;
		try {
			tmpDate = df.parse(basicDate);
		} catch (Exception e) {
			// 日期型字符串格式错误
		}
		long nDay = (tmpDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n) * (24 * 60 * 60 * 1000);
		tmpDate.setTime(nDay);

		return df.format(tmpDate);
	}
	
	public static Integer DayDiff(Date firstDay,Date secondDay){
		Long day = 60L * 60 * 24;
		Long diff = (firstDay.getTime() - secondDay.getTime()) / 1000;
		Long diffDay = diff / day;
		return diffDay.intValue();
	}
	
	public static void main(String[] args) {
		System.out.println(DateUtils.DayDiff(DateUtils.toDate("2011-09-22"), DateUtils.toDate("2011-03-25")));
	}
}
